'Copyright (c) 2008, Jonathan Allen

Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text

''' <summary>
''' The Request Token is used to gain permission to a user's account. 
''' </summary>
''' <remarks>
''' <list><item>Step 1: Generate a Request Token</item>
''' <item>Step 2: Have </item>
''' </list></remarks>
Public Class RequestToken
	Private m_Token As String
    Private m_TokenSecret As String
	Private m_PermissionUrl As String
	Private m_Converted As Boolean
	Private m_Connection As NetflixConnection
	Private m_RawData As String

	Friend ReadOnly Property RawData() As String
		Get
			Return m_RawData
		End Get
	End Property

	Friend Sub New(ByVal rawRequestToken As String, ByVal connection As NetflixConnection)
		Dim parsedData As New ClrExtensions.Net.Rest.QueryParameterCollection(rawRequestToken)
		m_Connection = connection
		m_RawData = rawRequestToken
		m_Token = parsedData("oauth_token")
		m_TokenSecret = parsedData("oauth_token_secret")
		m_PermissionUrl = NetflixUrls.PermissionLogin(connection, Me).ToUrlString
	End Sub

	''' <summary>
	''' This is the URL developers need to give to users in order to gain permission to access their account
	''' </summary>
	''' <value></value>
	''' <returns></returns>
	''' <remarks></remarks>
	Public ReadOnly Property PermissionUrl() As String
		Get
			Return m_PermissionUrl
		End Get
	End Property

	Friend ReadOnly Property TokenSecret() As String
		Get
			Return m_TokenSecret
		End Get
	End Property

	Friend ReadOnly Property Token() As String
		Get
			Return m_Token
		End Get
	End Property

	''' <summary>
	''' This method converts a Request Token into an Access Token
	''' </summary>
	''' <returns></returns>
	''' <remarks>Do not call this method until after the user approves your application. Calling it prematurely will will foul the Request Token.</remarks>
    Public Function ConvertToAccessToken() As AccessToken
        If m_Converted Then Throw New InvalidOperationException("This method can only be called once.")
        Dim accessToken As WrapNetflix.AccessToken = m_Connection.ConvertRequestToAccessToken(Me)
        m_Converted = True
        Return accessToken
    End Function

End Class
